home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c,comp.graphics.algorithms,rec.games.programmer
- Subject: Re: Speed question here...
- Date: 15 Feb 1996 07:34:47 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4fvjqnINN84p@keats.ugrad.cs.ubc.ca>
- References: <4ftluh$1gkv@hearst.cac.psu.edu>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <4ftluh$1gkv@hearst.cac.psu.edu>,
- William Koscho <koscho@wjk130.rh.psu.edu> wrote:
- >I was curious as to how fast something like the
- >following would execute:
-
- I don't know? How fast can your compiler tell you that the program can't be
- compiled?
-
- > int x;
- > node *ptr; ptr in linked list
- >
- > for ( ptr = first_node; ptr != NULL; ptr = ptr.next ) {
-
- You don't dereference pointers by doing ptr.field. You use *(ptr).field, or,
- more commonly, ptr->field.
-
-
- > for ( x = 0; x < max; x++ ) {
- > array[x] = array_other[x];
- > }
- > }
- >
- >I really am not interested in the assignment statement, that's easy.
- >but the traversal through the linked list, and inner integer incremental
- >for loop for each node visited. How fast is a traversal through
- >a linked list when you do a for loop at each node?
-
- How big is the for loop? The ptr variable (in a correctly coded version of the
- snippet that will compile when put into a proper program context) can be
- optimized into a register. The loop increment that passes through the array can
- be nothing more than just something like
-
- load R1, first_node
- loop:
- ; do whatever
-
- load R1, NEXT_OFFSET[R1] ; R1 = R1->next
- jnz R1, loop ; test R1, branch if zero
-
- This assumes a RISC machine with only indirect addressing.
-
- An empty inner loop (that isn't optimized away for some reason) won't have much
- of an effect on these instructions, but the speed of the traversal will
- obviously suffer if you do many iterations in the loop. The more work you do at
- each node, the less significant is the overhead of going through the list.
-
- --
-
-